package com.item.mobile;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.base.dialect.PaginationSupport;
import com.base.mobile.MobException;
import com.base.mobile.MobileInfo;
import com.base.mobile.server.annotation.Mobile;
import com.base.mobile.server.annotation.MobileMethod;
import com.base.service.BFileService;
import com.base.util.DateUtil;
import com.base.util.StringUtil;
import com.bf.common.proto.MCommon.MFile;
import com.bf.common.proto.MCommon.MFileList;
import com.bf.common.proto.MCommon.MFocus;
import com.bf.common.proto.MCommon.MFocusList;
import com.bf.common.proto.MCommon.MNotify;
import com.bf.common.proto.MCommon.MNotifyList;
import com.bf.common.proto.MCommon.MRet;
import com.bf.common.proto.MCommon.MSysParams;
import com.item.ConstantsCode;
import com.item.dao.model.Feedback;
import com.item.dao.model.Focus;
import com.item.dao.model.FocusExample;
import com.item.dao.model.FocusExample.Criteria;
import com.item.dao.model.UserNotify;
import com.item.dao.model.UserNotifyExample;
import com.item.service.CodeService;
import com.item.service.FeedbackService;
import com.item.service.FocusService;
import com.item.service.NewsService;
import com.item.service.UserCreditLogService;
import com.item.service.UserNotifyService;

@Mobile
@Component
public class MSystem {
	@Autowired
	private BFileService fileService;
	@Autowired
	private FeedbackService feedbackService;
	@Autowired
	private CodeService codeService;
	@Autowired
	private FocusService focusService;
	@Autowired
	private UserNotifyService userNotifyService;
	@Autowired
	private UserCreditLogService userCreditLogService;
	@Autowired
	private NewsService newsService;
	
	/**
	 * 上传文件
	 * @param requestProto
	 * @url /mobile?methodno=MUploadFile&debug=1&deviceid=test&appid=1&userid=&verify=
	 */
	@MobileMethod(methodno = "MUploadFile", isLogin = true, reqClz = MFileList.class)
	public MRet.Builder uploadFiles(MobileInfo mobileInfo, MFileList.Builder requestProto) throws Exception {
		if (requestProto == null) throw new MobException("files");
		MRet.Builder ret = MRet.newBuilder();
		ret.setCode(1);
		List<MFile.Builder> list = requestProto.getFileBuilderList();
		
		int max = list.size() - 1;

		if (max == -1){
			throw new MobException("files");
		}
		
		StringBuffer sb = new StringBuffer();
		
		for (int i = 0; ; i++){
			String temp = fileService.uploadFile(list.get(i).getFile().toByteArray(), list.get(i).getFileName(), mobileInfo.getUserid());
			sb.append(temp);
			if (i == max){
				ret.setMsg(sb.toString());
				return ret;
			}
			sb.append(",");
		}

	}
	
	/**
	 * 用户反馈
	 * @param content 内容
	 * @url /mobile?methodno=MFeedback&debug=1&deviceid=test&appid=1&content=
	 */
	@MobileMethod(methodno = "MFeedback", isLogin = false)
	public MRet.Builder feedback(MobileInfo mobileInfo, String content) throws Exception{
		// 参数校验
		if (StringUtils.isBlank(content)) {
			throw new MobException("content");
		}
		
		Feedback record = new Feedback();
		record.setCreateTime(new Date());
		record.setInfo(content);
		record.setUserId(mobileInfo.getUserid());
		feedbackService.insert(record);
		
		MRet.Builder ret = MRet.newBuilder();
		ret.setCode(0);
		return ret;
	}
	
	/**
	 * 获取系统常量
	 * @url /mobile?methodno=MSysParams&debug=1&deviceid=1
	 */
	@MobileMethod(methodno = "MSysParams", isLogin = false)
	public MSysParams.Builder sysParams(MobileInfo mobileInfo) throws Exception{
		MSysParams.Builder ret = MSysParams.newBuilder();
		ret.setParam1(codeService.getCode(ConstantsCode.WELCOME_IMG));//欢迎页
		return ret;
	}
	
	/**
	 * 获取系统常量
	 * @url /mobile?methodno=MSysParams&debug=1&deviceid=1
	 */
	@MobileMethod(methodno = "MSysParamByCode", isLogin = false)
	public MRet.Builder sysParamByCode(MobileInfo mobileInfo,String code) throws Exception{
		if (StringUtils.isBlank(code)) {
			throw new MobException("code");
		}
		MRet.Builder ret = MRet.newBuilder();
		ret.setCode(0);
		ret.setMsg(codeService.getCode(code));
		return ret;
	}
	
	/**
	 * 焦点图列表
	 * @param restraint //地区编号
	 * @param location //焦点图位置 1:首页
	 * @url /mobile?methodno=MFocusList&debug=1&deviceid=test&appid=1&content=
	 */
	@MobileMethod(methodno = "MFocusList", isLogin = false)
	public MFocusList.Builder focusList(MobileInfo mobileInfo, String restraint,Integer location) throws Exception{
		FocusExample example = new FocusExample();
		Criteria criteria = example.createCriteria();
		if(location!=null){
			criteria.andLocationEqualTo(location);
		}
		if(StringUtils.isNotBlank(restraint)){
			criteria.andRestraintEqualTo(restraint);
		}
		example.setOrderByClause("sort_order desc");
		List<Focus> list = focusService.selectByExample(example);
		MFocusList.Builder ret = MFocusList.newBuilder();
		for (Focus focus : list) {
			MFocus.Builder f = MFocus.newBuilder();
			f.setId(focus.getId());
			f.setImg(StringUtil.emptyIfBlank(focus.getImg()));
			f.setRedirectType(focus.getRedirectType());
			f.setRedirectContent(StringUtil.emptyIfBlank(focus.getRedirectContent()));
			if(focus.getRedirectType() == 2){
				f.setRedirectContent("/detail?type=2&id="+focus.getId());
			}
			ret.addList(f);
		}
		return ret;
	}
	
	/**
	 * 通知列表(需要登录,分页)
	 * @param type //类型 1:系统通知
	 * @param showType //显示类型 1:只显示标题 2:全部显示
	 * @url /mobile?methodno=MNotifyList&debug=1&appid=1&userid=&verify=&deviceid=&begin=
	 */
	@MobileMethod(methodno = "MNotifyList",isLogin=true,isPage=true)
	public MNotifyList.Builder notifyList(MobileInfo mobileInfo,Integer type,Integer showType,Integer page,Integer limit) throws Exception {
		// 参数校验
		if (showType == null) {
			showType = 1;
		}
		if (type == null) {
			throw new MobException("type");
		}
		UserNotifyExample example = new UserNotifyExample();
		example.setOrderByClause("create_time desc");
		example.createCriteria().andUserIdEqualTo(mobileInfo.getUserid()).andTypeEqualTo(type);
		PaginationSupport.byPage(page, limit,false);
		List<UserNotify> list = userNotifyService.selectByExample(example);
		
		MNotifyList.Builder ret = MNotifyList.newBuilder();
		for (UserNotify notify : list) {
			MNotify.Builder n = MNotify.newBuilder();
			n.setId(notify.getId());
			n.setTitle(StringUtil.emptyIfBlank(notify.getTitle()));
			n.setContent(showType == 1 ? "" : notify.getContent());
			n.setIsRead(notify.getIsRead());
			n.setCreateTime(DateUtil.getLnow(notify.getCreateTime()));
			n.setRedirectType(notify.getRedirectType());
			n.setRedirectContent(StringUtil.emptyIfBlank(notify.getRedirectContent()));
			ret.addNotify(n);
		}
		return ret;
	}

	/**
	 * 删除通知
	 * @param id 无则清空
	 * @param type //类型 1:系统通知
	 * @url /mobile?methodno=MDelNotify&debug=1&userid=&verify=&deviceid=&id=
	 */
	@MobileMethod(methodno = "MDelNotify",isLogin=true)
	public MRet.Builder delNotify(MobileInfo mobileInfo,String id,Integer type) throws Exception {
		if (type == null) {
			throw new MobException("type");
		}
		UserNotifyExample example = new UserNotifyExample();
		UserNotifyExample.Criteria criteria = example.createCriteria().andUserIdEqualTo(mobileInfo.getUserid()).andTypeEqualTo(type);
		if(StringUtils.isNotBlank(id)){
			criteria.andIdEqualTo(id);
		}
		userNotifyService.deleteByExample(example);
		MRet.Builder ret = MRet.newBuilder();
		ret.setCode(0);
		return ret;
	}
	
	/**
	 * 通知未读个数(需要登录)
	 * @param number type //类型 1:系统通知
	 * @url /mobile?methodno=MCountNotify&debug=1&userid=&verify=&deviceid=1&id=
	 */
	@MobileMethod(methodno = "MCountNotify",isLogin=true)
	public MRet.Builder countNotify(MobileInfo mobileInfo,Integer type) throws Exception {
		UserNotifyExample example = new UserNotifyExample();
		UserNotifyExample.Criteria criteria = example.createCriteria().andIsReadEqualTo(0).andUserIdEqualTo(mobileInfo.getUserid());;
		if (type != null) {
			criteria.andTypeEqualTo(type);
		}
		int cnt = userNotifyService.countByExample(example);
		
		MRet.Builder ret = MRet.newBuilder();
		ret.setCode(cnt);
		return ret;
	}
	
	/**
	 * 通知设为已读
	 * @param id
	 * @url /mobile?methodno=MReadNotify&debug=1&userid=&verify=&deviceid=1&id=
	 */
	@MobileMethod(methodno = "MReadNotify",isLogin=true)
	public MRet.Builder readNotify(MobileInfo mobileInfo,String id) throws Exception {
		if (StringUtils.isBlank(id)) {
			throw new MobException("id");
		}
		UserNotify record = new UserNotify();
		record.setId(id);
		record.setIsRead(1);
		userNotifyService.updateByPrimaryKeySelective(record);
		
		MRet.Builder ret = MRet.newBuilder();
		ret.setCode(0);
		return ret;
	}

	/**
	 * 通知详情
	 * @param id
	 * @url /mobile?methodno=MNotify&debug=1&userid=&verify=&deviceid=1&id=
	 */
	@MobileMethod(methodno = "MNotify",isLogin=true)
	public MNotify.Builder notify(MobileInfo mobileInfo,String id) throws Exception {
		if (StringUtils.isBlank(id)) {
			throw new MobException("id");
		}
		UserNotify notify = userNotifyService.selectByPrimaryKey(id);
		MNotify.Builder ret = MNotify.newBuilder();
		if(notify!=null){
			ret.setId(notify.getId());
			ret.setTitle(StringUtil.emptyIfBlank(notify.getTitle()));
			ret.setContent(StringUtil.emptyIfBlank(notify.getContent()));
			ret.setIsRead(notify.getIsRead());
			ret.setCreateTime(DateUtil.asHtml(notify.getCreateTime()));
			ret.setRedirectType(notify.getRedirectType());
			ret.setRedirectContent(StringUtil.emptyIfBlank(notify.getRedirectContent()));
		}
		return ret;
	}
	
}
